<template>
  <Panel>
    <template #title> <span>商品分类</span> </template>

    <template #content>
      <div>
        <!-- 1.0 按钮 -->
        <el-row>
          <el-col>
            <el-button type="primary" size="mini" @click="dialogVisible = true"
              >添加分类</el-button
            >
          </el-col>
        </el-row>
        <!-- 2.0 表格 -->
        <el-table :data="tableData" style="width: 100%">
          <el-table-column label="#" type="index" width="60"></el-table-column>
          <el-table-column label="分类名称" width="180">
            <template v-slot="{ row }">
              <span v-if="row.flag">{{ row.cateName }}</span>
              <el-input v-else v-model="row.cateName"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="是否启用" width="180">
            <template v-slot="{ row }">
              <el-switch
                :disabled="row.flag"
                :active-value="1"
                :inactive-value="0"
                v-model="row.state"
                active-color="#13ce66"
                inactive-color="#ff4949"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="{ row }">
              <el-button
                :type="row.flag ? 'primary' : 'success'"
                size="mini"
                @click="editCate(row)"
                >{{ row.flag ? '编辑' : '完成' }}</el-button
              >
              <el-button type="danger" size="mini" @click="delCate(row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 3.0 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[2, 5, 10, 15]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>

        <el-dialog title="添加分类" :visible.sync="dialogVisible" width="50%">
          <div>
            <el-form
              :model="cateForm"
              :rules="rules"
              ref="ruleForm"
              label-width="100px"
              class="demo-ruleForm"
            >
              <el-form-item label="分类名称" prop="cateName">
                <el-input v-model="cateForm.cateName"></el-input>
              </el-form-item>
              <el-form-item label="是否启用">
                <el-switch
                  v-model="cateForm.state"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                >
                </el-switch>
              </el-form-item>
            </el-form>
          </div>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addCate">确 定</el-button>
          </span>
        </el-dialog>
      </div>
    </template>
  </Panel>
</template>

<script>
import {
  getCateList,
  deleteCate,
  addCategory,
  editCategory
} from '../../api/pro.js'

export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      dialogVisible: false,
      cateForm: {
        cateName: '',
        state: 0
      },
      rules: {
        cateName: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getCates()
  },
  methods: {
    async getCates() {
      const { data, total } = await getCateList({
        pageSize: this.pageSize,
        currentPage: this.currentPage
      })
      // 给每一条数据定义一个 flag 将来可以操作当前行的数据
      data.forEach(item => {
        item.flag = true
      })
      this.tableData = data
      this.total = total
      console.log(data)
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getCates()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.currentPage = val
      this.getCates()
    },
    delCate(id) {
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const data = await deleteCate({ id })
          console.log(data)
          if (data.code === 0) {
            this.getCates()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    addCate() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          // 在这里发请求
          const data = await addCategory(this.cateForm)
          if (data.code === 0) {
            this.getCates()
            this.dialogVisible = false
          }
        } else {
          this.$message({
            message: '分类名称是必填项'
          })
          return false
        }
      })
    },
    async editCate(row) {
      if (row.flag) {
        row.flag = false
      } else {
        const data = await editCategory({
          id: row.id,
          cateName: row.cateName,
          state: Boolean(row.state)
        })
        console.log(data)
        if (data.code === 0) {
          row.flag = true
          this.getCates()
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin: 20px 0;
}
</style>
